"""
Case Type   : 资源池化系统视图
Case Name   : 开启按需回放+实时构建，查看系统视图dbe_perf.track_memory_context_detail
Create At   : 2025/2
Owner       : haomeng
Description :
    1、开启按需回放和实时构建特性
    2、主节点查看系统表dbe_perf.track_memory_context_detail中DMSDrcContext字段是否存在
    3、备节点查看系统表dbe_perf.track_memory_context_detail中DMSDrcContext字段是否存在
    4、恢复初始环境
Expect      :
    1、设置成功
    2、无相关信息
    3、无相关信息
    4、环境恢复成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class ShareStorageSystemView(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.prish = CommonSH('PrimaryDbUser')
        self.stash = CommonSH('Standby1DbUser')
        self.constant = Constant()

        self.param1 = 'recovery_parse_workers'
        self.param2 = 'recovery_redo_workers'
        self.param3 = 'ss_enable_ondemand_recovery'
        self.param4 = 'ss_ondemand_recovery_mem_size'
        self.param5 = 'ss_enable_ondemand_realtime_build'

        self.sys_view1 = 'dbe_perf.track_memory_context'
        self.sys_view2 = 'dbe_perf.track_memory_context_detail()'

    def test_ss_system_view(self):
        text = '-----step1:开启按需回放和实时构建特性  expect:设置成功-----'
        self.log.info(text)

        self.param_list = [self.param1, self.param2, self.param3, self.param4, self.param5]
        value_list = ['4', '4', 'on', '5GB', 'on']

        self.default_value = []
        for param in self.param_list:
            value = self.prish.execut_db_sql(f'show {param}')
            self.default_value.append(value.splitlines()[-2].strip())
        self.log.info(self.default_value)

        for param in self.param_list:
            set_param = self.prish.execute_gsguc('set',
                                              self.constant.GSGUC_SUCCESS_MSG,
                                              f'{param} = {value_list[self.param_list.index(param)]}')
            self.log.info(set_param)
            self.assertTrue(set_param, '参数设置失败')

        self.log.info('---重启集群使参数生效---')
        restart = self.prish.restart_db_cluster(False)
        self.log.info(restart)
        self.assertTrue(restart)

        self.log.info('---查看参数值修改是否成功---')
        for param in self.param_list:
            value = self.prish.execut_db_sql(f'show {param};')
            self.assertEqual(value.splitlines()[-2].strip(),
                             value_list[self.param_list.index(param)], '参数未修改成功')

        text = '-----step2:主节点查看系统表dbe_perf.track_memory_context_detail中' \
               'DMSDrcContext字段是否存在  expect:无相关信息-----'
        self.log.info(text)

        sql1 = f"select * from {self.sys_view1}('DMSDrcContext');"
        self.log.info(sql1)
        res1 = self.prish.execut_db_sql(sql1)
        self.log.info(res1)
        self.assertEqual('t', res1.splitlines()[-2].strip(), '存在异常信息')

        res1_2 = self.prish.execut_db_sql(f"select * from {self.sys_view2};",
                                          dbname='postgres')
        self.log.info(res1_2)
        self.assertIn('(0 rows)', res1_2, '存在异常信息')

        text = '-----step3:备节点查看系统表dbe_perf.track_memory_context_detail中' \
               'DMSDrcContext字段是否存在  expect:无相关信息-----'
        self.log.info(text)

        res2 = self.prish.execut_db_sql(sql1)
        self.log.info(res2)
        self.assertEqual('t', res2.splitlines()[-2].strip(), '存在异常信息')

        res2_2 = self.stash.execut_db_sql(f"select * from {self.sys_view2};",
                                          dbname='postgres')
        self.log.info(res2_2)
        self.assertIn('(0 rows)', res2_2, '存在异常信息')

    def tearDown(self):
        text = '-----step4:恢复初始环境  expect:恢复成功-----'
        self.log.info(text)

        for param in self.param_list:
            reset_param = self.prish.execute_gsguc('set',
                                                   self.constant.GSGUC_SUCCESS_MSG,
                                                   f'{param} = {self.default_value[self.param_list.index(param)]}')
            self.log.info(reset_param)

        restart = self.prish.restart_db_cluster(False)
        self.log.info(restart)

        for param in self.param_list:
            reset_value = self.prish.execut_db_sql(f'show {param};')
            self.log.info(reset_value)
            self.assertEqual(reset_value.splitlines()[-2].strip(),
                             self.default_value[self.param_list.index(param)], '参数恢复失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
